/***************************************************************************************************
 *
 * Project: Utilities
 *
 * Class: MPEGPlayer
 *
 * File name: MPEGPlayer.java
 *
 * Apr 21, 2008
 *
 **************************************************************************************************/

package com.eastmountain.sound;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.Player;

import com.eastmountain.chinese.db.exceptions.DBAccessException;
import com.eastmountain.chinese.db.flashcards.access.HZFindAccessor;
import com.eastmountain.chinese.db.flashcards.connection.MITDBAccessData;
import com.eastmountain.chinese.db.flashcards.records.ChineseElementProperties;

public class MPEGPlayer {

    private static HZFindAccessor hzFindAccessor;
    
    private static Player jzPlayer = null;   

    static public void play( String fileName, InputStream inputStream ) {
        
        System.out.println( "Now playing: " 
                            + fileName );
                            
        try {
            
            jzPlayer = new Player( inputStream );
            
        } catch ( JavaLayerException e ) {
        
            System.out.println( "*****  Unable to create MPEGPlayer" );

            // TODO Auto-generated catch block
            //e.printStackTrace();
        }
        
        try {
           
            jzPlayer.play();

            while ( ! jzPlayer.isComplete() ) {
                
                try {
                    Thread.sleep( 250 );
                } catch ( InterruptedException e ) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            
            try {
                Thread.sleep( 1000 );
            } catch ( InterruptedException e ) {
 
                System.out.println( "MPEGPlayer's sleep interrupted" );

                e.printStackTrace();
                
            }
            
            jzPlayer.close();
            jzPlayer = null;
            
        } catch ( JavaLayerException e ) {
            
            System.out.println( "*****  Unable to create MPEGPlayer" );
            
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        
        try {
            inputStream.close();
        } catch ( IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /***************************************************************************
     * 
     * main
     * 
     **************************************************************************/

    public static void main( String[] args ) {
        
        ChineseElementProperties chineseElementProperties;
        String level;
        
        String fileName;
        InputStream inputStream;
        
        System.out.println( "\n*****  About to start playing from SoundsDB\n" );     
        
        try {
            
            hzFindAccessor
                = new HZFindAccessor( new MITDBAccessData() );
            
        } catch ( DBAccessException e ) {
            
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
         
        System.out.println( "\n*****  MPEGPlayer about to start playing from Sounds DB\n" );     
        
        level = "T";
        
        while ( ( chineseElementProperties 
                      = hzFindAccessor.getNextWordRecordInSoundOrder( level ) ) != null ) {

            fileName    = chineseElementProperties.getPinyin();
            
            inputStream = chineseElementProperties.getSound();
                
            play( fileName, inputStream );
        
        }


    }

}
